<!-- AIGC项目跳到实训的落地页，用于登陆，复制 -->

<template>
  <div></div>
</template>

<script lang="ts" setup>
  import { storage } from 'g6-fn'
  import { useRoute, useRouter } from 'vue-router'

  import { personalApi } from '@/services'
  import { useUserInfoStore } from '@/store'

  const route = useRoute()
  const router = useRouter()

  const userInfoStore = useUserInfoStore()

  const redirectUrl = decodeURIComponent(route.query.redirectUrl as string)

  const init = async () => {
    const token = storage.get('token')
    if (!token) {
      router.push(`/login?redirectUrl=${encodeURIComponent(redirectUrl)}`)
      return
    }
    try {
      /* 判断token是否有效，token失效则清除token及用户信息 */
      await personalApi.verifyToken()
      const url = `${redirectUrl}?token=${token}`

      // 如果其他页面是往该平台首页跳转，则不携带token
      if (redirectUrl === '/') {
        location.href = redirectUrl
        return
      }

      location.href = url
    } catch (error) {
      userInfoStore.$reset()
      router.push(`/login?redirectUrl=${encodeURIComponent(redirectUrl)}`)
    }
  }

  init()
</script>

<style lang="less" scoped></style>
